<?php

namespace app\admin\model;

use think\Model;

/**
 * 用户访问模型
 * @package app\admin\model
 */
class UsersAccessLog extends Model
{
    /**
     * 当日活跃度(访问人数)
     * @param array $where
     * @return int
     */
    public static function todayBriskUsers($where = [])
    {
        return self::whereDay('create_time')->where($where)->count('distinct uid');
    }

    /**
     * 当日访问量(访问次数)
     * @return int
     */
    public static function todayAccess()
    {
        return self::whereDay('create_time')->count();
    }

    /**
     * 本月访问数据
     * @return array
     */
    public static function getUsersAccessMonth()
    {
        $data = self::fieldRaw("COUNT(*) total,create_time")
            ->whereMonth('create_time')
            ->group('create_time')
            ->select();
        return $data ? $data->toArray() : [];
    }

    /**
     * 首次访问时间
     * @param $where
     * @return mixed
     */
    public static function findUsersAccessTime($where)
    {
        return self::where($where)->order('create_time', 'desc')->value('create_time');
    }

    /**
     * 统计总访问次数
     * @param $where
     * @return int
     */
    public static function countUsersAccess($where)
    {
        return self::where($where)->count();
    }

    /**
     * 统计本月访问
     * @param array $where
     * @return int
     */
    public static function countUsersAccessMonth($where = [])
    {
        return self::whereMonth('create_time')->where($where)->count();
    }

    /**
     * 统计今年访问
     * @param array $where
     * @return int
     */
    public static function countUsersAccessYear($where = [])
    {
        return self::whereYear('create_time')->where($where)->count();
    }

    /**
     * 本周访问数据
     * @return array
     */
    public static function getUsersAccessWeek()
    {
        $data = self::fieldRaw("COUNT(*) total,create_time")
            ->whereWeek('create_time')
            ->group('create_time')
            ->select();
        return $data ? $data->toArray() : [];
    }

    /**
     * 统计本周访问
     * @param array $where
     * @return int
     */
    public static function countUsersAccessWeek($where = [])
    {
        return self::whereWeek('create_time')->where($where)->count();
    }

}